import { AnalysisExportStudent, CourseAnalysis } from "../../../../../utils/request/api";
import msg from "../../../../../utils/msg";
import User from "../../../../../utils/user";
import { Course } from "miniprogram/pages/curriculum/interface";
interface absentStudent{name:string,absentRate:number,code:string}
// pages/user/s_worker/chooseTime/analysis/analysis.ts
Page({

    /**
     * 页面的初始数据
     */
    data: {
        course:{}as any,
        all: {} as { normal: number, late: number, absent: number, others: number },
        weeks: {} as { [key: string]: { normal: number, late: number, absent: number, number: number, week: number, day: number } },
        students: {} as { [key: string]: { name: string, absent: number, need: number,normal:number } },
        //第几周
        week: 0 as number,
        //已经上了几节课(统计的)
        analysisNum: 0 as number,
        all_signRate: 100 as number,
        normal_signRate: 0 as number,
        //所需签到人次
        needSign: 0 as number,
        //实际签到人次
        countSign: 0 as number,
        //总请假人次
        leave: 0 as number,
        //迟到率
        lateRate:0 as number,
        //请假率
        leaveRate:0 as number,
        //缺勤率（学生）
        absentRate:20 as number,
        //
        absentStudents:[] as absentStudent[],
        absentStudents_show:[] as absentStudent[],
        //
        all_course_signRate:[] as {key:string,signRate:number}[],
    },
    //导出名单
    export:async function(){
        await msg.confirm('是否导出学生数据','');
        msg.Loading();
        let filename = this.data.course.c_name+'第'+this.data.week+'周 签到情况报表';
        await AnalysisExportStudent({data:{course_id:this.data.course.course_id},fileName:filename});
        msg.hideLoading();
    },
    //拖动进度条
    onDrag(event:any){
        let rate=event.detail.value;
        if(!rate) rate=event.detail;
        this.setData({
            absentRate:rate,
        });
        this._getAbsentStudentsShow(rate);
    },
    _getAbsentStudentsShow(absentRate:number){
        let absentStudents_show:absentStudent[]=[];
        let absentStudents=this.data.absentStudents;
        for(let i=0;i<absentStudents.length;i++){
            if(absentRate<=absentStudents[i].absentRate) absentStudents_show.push(absentStudents[i]);
            else break;
        }
        this.setData({
            absentStudents_show:absentStudents_show,
        });
    },
    //计算学生们的缺勤率并排序
    _getAbenstStudents(){
        let students = this.data.students;
        let absentStudents:absentStudent[]=[];
        for(let code in students){
            let absentRate=(students[code].absent*100/students[code].need);
            absentStudents.push({code:code,name:students[code].name,absentRate:Number(absentRate.toFixed(1))})
        }
        //ascPersons = persons.sort((a, b) => a.age - b.age);
        let absentStudents_sorted:absentStudent[]=absentStudents.sort((a, b) => b.absentRate - a.absentRate);
        this.setData({
            absentStudents:absentStudents_sorted,
        });
    },
    onClickLeft() {
        wx.navigateBack();
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: async function (options: any) {
        let course = JSON.parse(options.course);
        console.log('course=',course);
        let course_id=course.course_id;
        this.setData({course:course});
        let user = new User;
        msg.Loading('获取数据中');
        let res = await CourseAnalysis({ course_id: course_id, openid: user.openid })
            .then((res: any) => {
                return res;
            })
            .catch((err: any) => { console.log('err=', err) });
        this._init(res);
    },
    _init(res: any) {
        msg.Loading('计算中');
        let all = res.data.data.all;
        let weeks = res.data.data.weeks;
        let students = res.data.data.students;
        let week = res.data.data.week;
        this.setData({
            all: all,
            weeks: weeks,
            students: students,
            week: week,
        });
        //获取:统计了几节课的数据
        this._getAnalysisNum();
        let all_num = (this.data.all.normal + this.data.all.absent + this.data.all.late);
        //计算整体缺勤率
        let all_signRate = (this.data.all.absent / all_num) * 100;
        //计算正常签到率
        let normal_late = (this.data.all.normal + this.data.all.late);
        if (normal_late == 0) normal_late = 1;
        let normal_signRate = (this.data.all.normal / normal_late) * 100;
        this.setData({
            all_signRate: Number(all_signRate.toFixed(2)),
            normal_signRate: Number(normal_signRate.toFixed(2)),
        });
        this._calData();//
        this._getAbenstStudents();//
        this._getAbsentStudentsShow(20);
        this._get_all_course_signRate();
        msg.hideLoading();
    },
    //
    _get_all_course_signRate(){
        let weeks = this.data.weeks;
        let all_course_signRate:{key:string,signRate:number}[]=[];
        for(let key in weeks){
            let temp = key.split('-');
            let signRate=(weeks[key].normal+weeks[key].late)*100/(weeks[key].absent+weeks[key].normal+weeks[key].late);
            if((weeks[key].absent+weeks[key].normal+weeks[key].late)==0) signRate=100;
            all_course_signRate.push({key:'第'+temp[0]+'周星期'+temp[1],signRate:Number(signRate.toFixed(1))});
        }
        this.setData({
            all_course_signRate:all_course_signRate,
        })
    },
    _calData() {
        let all = this.data.all;
        let needSign = all.late + all.absent + all.normal;
        let countSign = all.late  + all.normal;
        let lateRate = all.late*100/(all.late  + all.normal);
        lateRate=Number(lateRate.toFixed(2));
        if(!lateRate) lateRate=0;
        let leaveRate = all.others*100/(all.late  + all.normal+ all.absent+all.others);
        if(!leaveRate) leaveRate=0;
        leaveRate=Number(leaveRate.toFixed(2));
        this.setData({
            needSign: needSign,
            countSign:countSign,
            leave:all.others,
            lateRate:lateRate,
            leaveRate:leaveRate,
        });
    },
    //
    _getAnalysisNum() {
        let data = this.data.weeks;
        let num = 0;
        for (let key in data) {
            num++;
        }
        this.setData({
            analysisNum: num,
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})